package com.mofofa.maozy.launch

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import com.mofofa.maozy.ProjectApplication
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseViewModel
import com.mofofa.maozy.data.*
import com.mofofa.maozy.network.ApiUserReposity
import com.mofofa.maozy.utils.UserStateUtil

/** 用户流程viewModel */
class UserStateViewModel : BaseViewModel() {

    /** 权限列表 */
    var mMaozyPermissionMaozyList = mutableListOf<DataPermission>()

    /** 查询app配置回调 */
    private val mMaozyFetchMaozySysMaozyConfigMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 查询app版本回调 */
    private val mMaozyFetchMaozyAppMaozyVersionMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 查询登录otp回调 */
    private val mMaozyFetchMaozyOtpMaozyLiveData = MutableLiveData<ReqDoLogin>()

    /** 登录回调 */
    private val mMaozyDoMaozyLoginMaozyLiveData = MutableLiveData<ReqDoLogin>()

    /** 查询用户状态回调 */
    private val mMaozyFetchMaozyUserMaozyStateMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 查询用户状态回调-点下一步按键时调用 */
    private val mMaozyFetchMaozyUserMaozyStateMaozyForMaozyCommitMaozyActionMaozyLiveData =
        MutableLiveData<ReqBase>()

    /** 查询app配置回调 */
    val onMaozyFetchMaozySysMaozyConfigMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozySysMaozyConfigMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyAppMaozyConfig(this, query)
        }

    /** 查询app版本回调 */
    val onMaozyFetchMaozyAppMaozyVersionMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyAppMaozyVersionMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyAppMaozyVersion(this, query)
        }

    /** 初始化审核账号回调 */
    val onMaozyInitMaozyAccMaozyUserMaozyLiveData = MutableLiveData<Boolean>()

    /** 查询登录otp回调 */
    val onMaozyFetchMaozyOtpMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyOtpMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyOtp(this, query)
        }

    /** 登录回调 */
    val onMaozyDoMaozyLoginMaozyLiveData =
        Transformations.switchMap(mMaozyDoMaozyLoginMaozyLiveData) { query ->
            ApiUserReposity.doMaozyLogin(this, query)
        }

    /** 查询用户状态回调 */
    val onMaozyFetchMaozyUserMaozyStateMaozyLiveData =
        Transformations.switchMap(mMaozyFetchMaozyUserMaozyStateMaozyLiveData) { query ->
            ApiUserReposity.fetchMaozyUserMaozyState(this, query)
        }

    /** 查询用户状态回调-点下一步按键时调用 */
    val onMaozyFetchMaozyUserMaozyStateMaozyForMaozyCommitMaozyActionMaozyLiveData =
        Transformations.switchMap(
            mMaozyFetchMaozyUserMaozyStateMaozyForMaozyCommitMaozyActionMaozyLiveData
        ) { query ->
            ApiUserReposity.fetchMaozyUserMaozyState(this, query)
        }

    /** 查询app配置 */
    fun fetchMaozySysMaozyConfig() {
        mMaozyFetchMaozySysMaozyConfigMaozyLiveData.value = ReqBase()
    }

    /** 查询app版本 */
    fun fetchMaozyAppMaozyVersion() {
        mMaozyFetchMaozyAppMaozyVersionMaozyLiveData.value = ReqBase()
    }

    /** 查询登录otp */
    fun fetchMaozyOtp(reqDoLogin: ReqDoLogin) {
        mMaozyFetchMaozyOtpMaozyLiveData.value = reqDoLogin
    }

    /** 登录 */
    fun doMaozyLogin(reqDoLogin: ReqDoLogin) {
        mMaozyDoMaozyLoginMaozyLiveData.value = reqDoLogin
    }

    /** 查询用户状态 */
    fun fetchMaozyUserMaozyState() {
        mMaozyFetchMaozyUserMaozyStateMaozyLiveData.value = ReqBase()
    }

    /** 查询用户状态-点下一步按键时调用 */
    fun fetchMaozyUserMaozyStateMaozyForMaozyCommitMaozyAction(reqBase: ReqBase) {
        mMaozyFetchMaozyUserMaozyStateMaozyForMaozyCommitMaozyActionMaozyLiveData.value =
            reqBase
    }

    /** 初始化权限列表 */
    fun initMaozyPermissionMaozyList() {
        mMaozyPermissionMaozyList.clear()
        mMaozyPermissionMaozyList.add(
            DataPermission(
                imgRes = R.drawable.maozy_permission_phone,
                title = ProjectApplication.context.getString(R.string.maozy_permission_phone),
                detail = ProjectApplication.context.getString(R.string.maozy_permission_phone_des)
            )
        )
        mMaozyPermissionMaozyList.add(
            DataPermission(
                imgRes = R.drawable.maozy_permission_sms,
                title = ProjectApplication.context.getString(R.string.maozy_permission_sms),
                detail = ProjectApplication.context.getString(R.string.maozy_permission_sms_des)
            )
        )

        //最后添加data safty
        mMaozyPermissionMaozyList.add(
            DataPermission(
                imgRes = R.drawable.maozy_permission_calendar,
                title = ProjectApplication.context.getString(R.string.maozy_permission_data_security),
                detail = ProjectApplication.context.getString(R.string.maozy_permission_data_security_des)
            )
        )
    }

    /** 初始化审核账号 */
    fun initMaozyAccMaozyUser(b4dmtbepk5hgxvt9jxryn3a: DataReviewAccount) {
        if (b4dmtbepk5hgxvt9jxryn3a.accTk.isBlank()) {
            onMaozyInitMaozyAccMaozyUserMaozyLiveData.value = false
        } else {
            //更新登录状态
            UserStateUtil.updateMaozyLoginMaozyState(
                RevObjDoLogin(
                    token = b4dmtbepk5hgxvt9jxryn3a.accTk,
                    userId = b4dmtbepk5hgxvt9jxryn3a.accTk,
                    mobile = b4dmtbepk5hgxvt9jxryn3a.accM,
                    type = "OLD",
                    directLoginFlag = "NO"
                )
            )
            onMaozyInitMaozyAccMaozyUserMaozyLiveData.value = true
        }
    }
}